<template>
	<view>
    <!-- 自定义搜索组件 -->
    <my-search @click="gotoSearch"></my-search>
		<view class="scroll-view-container">
      <!-- 左侧的滑动区域 -->
		  <scroll-view scroll-y="true" class="left-scroll" :style="{height:wh + 'px'}">
        <block v-for="(item, i) in cateList" :key="i">
          <view :class="['left-scroll-view-item', i === active ? 'active' : '']" @click="activeChanged(i)">{{item.cat_name}}</view>
        </block>
		  </scroll-view>
      <!-- 右侧的滑动区域 -->
      <scroll-view scroll-y="true" class="right-scrool" :style="{height:wh + 'px'}" :scroll-top="scrollTop">
        <!-- 二级分类的数据 -->
        <view class="cate-le2" v-for="(item2, i2) in cateLevel2" :key="i2">
          <!-- 二级分类的标题 -->
          <view class="cate-le2-title">/ {{item2.cat_name}} /</view>
          <!-- 二级分类下的三级分类 -->
          <view class="cate-le3-list">
            <!-- 三级分类的每一个数据 -->
            <view class="cate-le3-item" v-for="(item3, i3) in item2.children" :key="i3" @click="gotoGoodsList(item3)">
              <image :src="item3.cat_icon"></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
		</view>
	</view>
</template>

<script>
  import tabbarMix from '@/mixins/tabbar-badge.js'
	export default {
    mixins: [tabbarMix],
		data() {
			return {
        // 设备可用的高度
				wh: 0,
        // 分类列表的数据
        cateList: [],
        // 高亮显示的条件
        active: 0,
        // 2级分类的数据
        cateLevel2: [],
        // 滚动条距离顶部的距离
        scrollTop: 0
			};
		},
    onLoad() {
      const syInfo = uni.getSystemInfoSync()
      this.wh = syInfo.windowHeight - 50
      this.getCateList()
    },
    methods: {
      // 获取列表分类数据的方法
      async getCateList () {
        const {data: res} = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        // 2级分类赋值
        this.cateLevel2 = res.message[0].children
      },
      // 点击高亮显示
      activeChanged (i) {
        this.active = i
        this.cateLevel2 = this.cateList[i].children
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },
      // 每个商品的详情
      gotoGoodsList (item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      // 点击跳转到搜索页面
      gotoSearch () {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
	}
</script>

<style lang="scss">
.scroll-view-container{
  display: flex;
  .left-scroll{
    width: 120px;
  }
}
.left-scroll-view-item{
  line-height: 60px;
  background-color: #F7F7F7;
  text-align: center;
  font-size: 12px;
  &.active{
    background-color: #FFFFFF;
    position: relative;
    &::before{
      content: " ";
      height: 30px;
      width: 3px;
      display: block;
      background-color: #C00000;
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
    }
  }
}
.cate-le2-title{
  text-align: center;
  color: #C00000;
  padding: 15px 0;
}
.cate-le3-list{
  display: flex;
  flex-wrap: wrap;
  .cate-le3-item{
    width: 33.33%;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 10px;
    image{
      width: 60px;
      height: 60px;
    }
    text{
      font-size: 12px;
    }
  }
}
</style>
